package cn.edu.scau.cmi.pengzhijie.spring.controller;


import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import org.hibernate.type.IntegerType;
import org.springframework.context.ApplicationContext;


import cn.edu.scau.cmi.pengzhijie.hibenate.dao.StudentDao;
import cn.edu.scau.cmi.pengzhijie.hibenate.dao.TeacherDao;
import cn.edu.scau.cmi.pengzhijie.hibenate.domain.Student;
import cn.edu.scau.cmi.pengzhijie.hibenate.domain.Teacher;
import cn.edu.scau.cmi.pengzhijie.spring.util.ApplicationContextUtil;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Pane;

public class SpringController implements Initializable{
	private ApplicationContext context;
	private String str = "";
	private Teacher teacher = new Teacher();
	private Student student = new Student();
	private StudentDao studentDao;
	private TeacherDao teacherDao;
	List studentlist = null;
	List teacherlist = null;
	@FXML private Pane pane;
    @FXML private TextField studentName;
    @FXML private TextField studentNumber;
    @FXML private TextField studentId;
    @FXML private TextField tutorId;
    @FXML private TextField textTeacherId;
    @FXML private TextField teacherId;
    @FXML private TextField teacherName;
    @FXML private Button sendStudent;
    @FXML private TextArea info;
    
    private void initList() {
    	this.teacherDao = (TeacherDao) context.getBean("teacherdao");
    	this.studentDao = (StudentDao) context.getBean("studentdao");
		studentlist = studentDao.listAll();
		teacherlist = teacherDao.listAll();
		str += "当前数据学生数据有:\n";
		for(int i = 0 ; i < studentlist.size(); i++) {
			student = (Student) studentlist.get(i);
			str += "id号: " + i + "  学生姓名:" + student.getName() + "  学号: " + student.getNumber();
			if(student.getTeacher() != null) {
				str += "  导师为: " + student.getTeacher().getName();
			}
			str += "\n";
		}
		str += "当前数据老师数据有:\n";
		for(int i = 0 ; i < teacherlist.size(); i++) {
			teacher = (Teacher) teacherlist.get(i);
			str += "id号: " + i + "  学生姓名:" + teacher.getName() + "\n";
		}
		info.setText(str);
	}
    
    public SpringController() {
		// TODO Auto-generated constructor stub
    	this.context = ApplicationContextUtil.getApplicationContext();
 
	}

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		
	}
    @FXML
    public void createStudent() {
    	String studentname = studentName.getText();
    	int number= Integer.parseInt(studentNumber.getText());
    	String tutor = textTeacherId.getText();
    	int tutornumber = -1;
    	if(!tutor.equals("")) {
    		tutornumber = Integer.parseInt(tutor);
    	}
    	System.out.println(tutornumber);
    	this.studentDao = (StudentDao) context.getBean("studentdao");
    	student.setName(studentname);
    	student.setNumber(number);
    	if(tutornumber != -1) {
    		teacher = (Teacher) teacherlist.get(tutornumber);
    		student.setTeacher(teacher);
    	}
    	studentDao.save(student);
    	str += "  学生姓名:" + student.getName() + "  学号: " + student.getNumber() + "   创建成功\n";
    	info.setText(str);
       	initList();
    }
    @FXML
    public void deleteStudent() {
    	int deleteStudentId = Integer.parseInt(studentId.getText());
    	student = (Student) studentlist.get(deleteStudentId);
    	this.studentDao = (StudentDao) context.getBean("studentdao");
    	studentDao.delete(student);
    	str += "删除成功\n";
    	info.setText(str);
    	initList();
    }
    @FXML
    public void changeStudent() {
    	int studentnumber = Integer.parseInt(studentId.getText());
    	student = (Student)studentlist.get(studentnumber);
    	String studentname = studentName.getText();
    	int number= Integer.parseInt(studentNumber.getText());
    	String tutor = textTeacherId.getText();
    	int tutornumber = -1;
    	if(!tutor.equals("")) {
    		tutornumber = Integer.parseInt(tutor);
    	}
    	System.out.println(tutornumber);
    	this.studentDao = (StudentDao) context.getBean("studentdao");
    	student.setName(studentname);
    	student.setNumber(number);
    	if(tutornumber != -1) {
    		teacher = (Teacher) teacherlist.get(tutornumber);
    	}
    	str += "老师为: " + teacher.getName();
		student.setTeacher(teacher);
    	studentDao.updata(student);
    	str += "  学生姓名:" + student.getName() + "  学号: " + student.getNumber() + "   修改成功\n";
    	info.setText(str);
       	initList();
    }
    @FXML
    public void createTeacher() {
    	String teachername = teacherName.getText();
    	int teacherid = Integer.parseInt(textTeacherId.getText());
    	this.teacherDao = (TeacherDao) context.getBean("teacherdao");
    	System.out.println(teachername);
    	teacher.setName(teachername);
    	teacher.setId(teacherid);
    	teacherDao.save(teacher);
    	str += "  老师姓名:" + teacher.getName() + "   创建成功\n";
    	info.setText(str);
    	initList();
    }
    @FXML
    public void deleteTeacher() {
    	int deleteTeacherId = Integer.parseInt(teacherId.getText());
      	this.teacherDao = (TeacherDao) context.getBean("teacherdao");
      	teacher = (Teacher) teacherlist.get(deleteTeacherId);
    	teacherDao.delete(teacher);
    	str += "  老师姓名:" + teacher.getName() + "   删除成功\n";
    	initList();
    }
    @FXML
    public void changeTeacher() {
    	int teachernumber = Integer.parseInt(teacherId.getText());
    	teacher = (Teacher) teacherlist.get(teachernumber);
    	String teachername = teacherName.getText();
    	this.teacherDao = (TeacherDao) context.getBean("teacherdao");
    	System.out.println(teachername);
    	teacher.setName(teachername);
    	teacherDao.updata(teacher);
    	str += "  老师姓名:" + teacher.getName() + "   修改成功\n";
    	info.setText(str);
    	initList();
    }
}
